<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Log extends AppController
{
	public function admin_index($sortBy = 'id',$sortOrder = 'desc'){
		
		$pag  = array(
			'table' => 'Log',
			'uri_segment' => 6,
			//de sau
			'sort' => true,
			//so luong item tren 1 page
			'per_page' =>10,
			//de say
			'sortList'=>array('stt'=>'id','Người Dùng'=>'shUser','Đối Tượng'=>'object','Chi Tiết'=>'message','date'=>'date'),
			'base_url' => '/admin/log/index',
			//de sau
			'sortBy' => $sortBy, 'sortOrder' => $sortOrder
		);

		$pag = $this->paginate($pag);
		$pag['total_rows'] =  $this->getEM()->getRepository('\Entity\ShLog')->total_rows($pag);
		$this->pagination->initialize($pag);
		$this->data['pagination_link'] = $this->pagination->create_links();
		
		$this->data['logs'] =  $this->getEM()->getRepository('Entity\ShLog')->backendPaginate($pag);
		$this->render('log/admin_index',array('back.js'));
	}
	public function admin_view($id = null){
		$id = (int)$id;
		if(empty($id) || !is_int($id)):
			$this->session->set_flashdata('error','Sai mã lịch sử');
		redirect('admin/page');
		endif;

		$l = $this->getEM()->getRepository('Entity\ShLog')->findOneById($id);
		if(empty($l)):
			$this->session->set_flashdata('error','Không Có lịch sử truy cập này');
		redirect('admin/page');
		endif;
		$this->data['log'] = $this->getEM()->getRepository('Entity\ShLog')->getLogBy($l);
		$this->render('log/admin_view');
	}
	public function admin_delete($url ='admin'){
		
		$l = $this->getEM()->getRepository('Entity\ShLog')->deleteAll();
		if($l):
			$this->log(15,array($this->session->set_userdata['user']));
			$this->session->set_flashdata('success','Xóa Thành Công');
		else:
			$this->session->set_flashdata('error','Lỗi Sql thử lại');
		endif;
		redirect('/'.$url.'/page');
	}
	
}